﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Security.Cryptography;

public partial class User_ChangePass : System.Web.UI.Page
{
    MemberBUS memBUS = new MemberBUS();
    DataTable tbMem = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ID"] == null)
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        if (txtNPass.Text.Equals("") || txtOPass.Text.Equals("") || txtRetype.Text.Equals(""))
        {
            lblError.Text = "You must enter enough information";
        }
        else
        {
            tbMem = memBUS.isvalidLogin(Session["User"].ToString(),Md5AddSecret(txtOPass.Text));
            if (tbMem.Rows.Count > 0)
            {
                if (txtNPass.Text.Equals(txtRetype.Text))
                {
                    bool check = memBUS.updatePass(Session["ID"].ToString(), Md5AddSecret(txtNPass.Text));
                    if (check)
                    {

                        lblError.Text = "Change password successful !";
                    }
                }
                else
                {
                    lblError.Text = "Password same as Retype";

                }

            }
            else
            {
                lblError.Text = "Password wrong";

            }
        }
    }
    public string Md5AddSecret(string strChange)
    {
        //Change the syllable into UTF8 code
        byte[] pass = Encoding.UTF8.GetBytes(strChange);
        MD5 md5 = new MD5CryptoServiceProvider();
        string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
        return strPassword;
    }
}